<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * news short summary.
 *
 * news description.
 *
 * @version 1.0
 * @author xianbin
 */
class news extends Menu_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->Model('New_model');
        $this->load->Model('Menu_model');
        $this->load->library('ajaxMsgModel');
        $this->load->library('form_validation');
    }
    public function newslist()
    {
        $this->ajaxmsgmodel->Type='1';
        $this->Ajax_Check($this->ajaxmsgmodel);
        $cupge=$this->input->get('page');
        $cupge=empty($cupge) || $cupge=="0"?1:$cupge;
        $menuid=$this->uri->segment(4);

        $data['crumbs_items']=$this->get_crumbs_content_menu($menuid);
        $pagesize=10;
        $new_page_objs=$this->New_model->get_page($cupge,$pagesize,"a.*","new","order by weight desc, adddate desc, new_id desc ","new_id","where menuid=$menuid","");
        $this->load->library('page');
        $p=new Page();
        $data['menuid']=$menuid;
        $data['newslist']=arrayToObject($new_page_objs['objlist']);
        $p->init($new_page_objs['count'][0]->_count,5,$cupge,$pagesize,site_url('admin/news/newslist/'.$menuid));
        $data['links']=$p->showPages(1);
        $this->load->view('admin/news/newslist.html',$data);
    }

    public function addnew()
    {
        $this->ajaxmsgmodel->Type='1';
        $this->Ajax_Check($this->ajaxmsgmodel);
        $menuid=$this->uri->segment(4);
        $data['crumbs_items']=$this->get_crumbs_content_menu($menuid);
        $data['menuid']=$menuid;
        $this->load->view('admin/news/addnew.html',$data);
    }

    public function add_new()
    {
        $this->ajaxmsgmodel->Type='4';
        $this->Ajax_Check($this->ajaxmsgmodel);

        $newModel= new $this->New_model;
        $newModel->title=$this->input->post('title');
        $newModel->contents=$this->input->post('contents',false);
        $newModel->imgurl=$this->input->post('imgurl');
        $newModel->menuid=$this->input->post('menuid');
        $newModel->weight=$this->input->post('weight');
        $newModel->adddate=$this->input->post('adddate');
        $newModel->keyword=$this->input->post('keyword');
        $newModel->description=$this->input->post('description');

        $res=$this->New_model->add_new($newModel);
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='添加成功';
            $this->ajaxmsgmodel->Statu='ok';
        }
        else
        {
            $this->ajaxmsgmodel->Msg='添加失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function editnew()
    {
        $this->ajaxmsgmodel->Type='1';
        $this->Ajax_Check($this->ajaxmsgmodel);
        $new_id=$this->uri->segment(4);
        $new_model=$this->New_model->get_new_by_id($new_id);
        $data['newModel']=$new_model;
        $data['crumbs_items']=$this->get_crumbs_content_menu($new_model->menuid);
        $data['menuid']=$new_model->menuid;
        $this->load->view('admin/news/editnew.html',$data);
    }

    public function edit_new()
    {
        $this->ajaxmsgmodel->Type='4';
        $this->Ajax_Check($this->ajaxmsgmodel);

        $newModel= new $this->New_model;
        $newModel->title=$this->input->post('title');
        $newModel->contents=$this->input->post('contents',false);
        $newModel->imgurl=$this->input->post('imgurl');
        $newModel->new_id=$this->input->post('new_id');
        $newModel->weight=$this->input->post('weight');
        $newModel->adddate=$this->input->post('adddate');
        $newModel->keyword=$this->input->post('keyword');
        $newModel->description=$this->input->post('description');

        $res=$this->New_model->update_new($newModel);
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='修改成功';
            $this->ajaxmsgmodel->Statu='ok';
        }
        else
        {
            $this->ajaxmsgmodel->Msg='修改失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function set_new_weight()
    {
        $this->load->library('ajaxMsgModel');
        $newModel=new $this->New_model();
        $newModel->new_id=$this->uri->segment(4);
        $newModel->weight=$this->input->post('weight');
        $backurl=$this->input->get('returnurl');
        $res=$newModel->set_new_weight($newModel);
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='更新成功';
            $this->ajaxmsgmodel->Statu='ok';
            $this->ajaxmsgmodel->BackUrl=$backurl;
        }
        else
        {
            $this->ajaxmsgmodel->Msg='更新失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function del_new()
    {
        $this->ajaxmsgmodel->Type='4';
        $this->Ajax_Check($this->ajaxmsgmodel);

        $new_id=$this->uri->segment(4);
        $back_url=$this->input->get('returnurl');
        $res=$this->New_model->del_new($new_id);
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='删除成功';
            $this->ajaxmsgmodel->Statu='ok';
            $this->ajaxmsgmodel->BackUrl=$back_url;
        }else{
            $this->ajaxmsgmodel->Msg='删除失败';
            $this->ajaxmsgmodel->Statu='ok';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function batch_del_new()
    {
        $this->ajaxmsgmodel->Type='4';
        $this->Ajax_Check($this->ajaxmsgmodel);

        $nids=$this->input->post('items');
        $backurl=$this->input->get('returnurl');
        $newModel=new $this->New_model();
        $nidsArray=explode('|',trim($nids,'|'));
        if(count($nidsArray)>0)
        {
            $res=$newModel->batch_del_new($nidsArray);
            if($res>0)
            {
                $this->ajaxmsgmodel->Msg='删除成功';
                $this->ajaxmsgmodel->Statu='ok';
                $this->ajaxmsgmodel->BackUrl=$backurl;
            }
            else
            {
                $this->ajaxmsgmodel->Msg='删除失败';
                $this->ajaxmsgmodel->Statu='err';
            }
        }
        else
        {
            $this->ajaxmsgmodel->Msg='请选择删除项！';
            $this->ajaxmsgmodel->Statu='err';
        }

        echo(json_encode($this->ajaxmsgmodel));
    }
}